<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
model('jy_weishop/member')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$return['set']=$this->setting;
	$this->model()->update_agent_level($_SESSION['uid']);	
	$return['member']=pdo_get('jy_weishop_member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$return['member']){
		//exi('您还不是会员,请先成为会员','','jy_weishop/member.index/');
	}	
	if($return['member']['ispartner']!=1 || $return['member']['partnerstatus']!=1){
		exi('您还不是股东!','','/register/');
	}
	$return['member']['nickname']=$_SESSION['member']['nickname'];
	$return['member']['avatar']=$_SESSION['member']['avatar'];
	if($return['member']['partnerlevel']==0){
		$return['member']['levelname']='默认等级';
		$return['member']['bonus']=0;
	}else{
		$return['member']['levelname']=pdo_getcolumn('jy_globonus_level',array('merchid'=>$_SESSION['onemerchid'],'id'=>$return['member']['partnerlevel']),'levelname');
		$return['member']['bonus']=pdo_getcolumn('jy_globonus_level',array('merchid'=>$_SESSION['onemerchid'],'id'=>$return['member']['partnerlevel']),'bonus');
	}
	if($return['set']['zhouqi']!=2){
		$firstday =  strtotime(date("Y-m-01", time()));
	}else{
		$day=date("d", time());
		$firstday = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		$firstday =strtotime($firstday);
	}
	if($return['member']['partnertime']>$firstday){
		$firstday=$return['member']['partnertime'];
	}
	$totalmoney=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$firstday),'price');
	if(!$list['money']){
		$list['money']=0;
	}
	if(!$total['money']){
		$total['money']=0;
	}
	$rate=$this->setting['rate']?:10;
	$return['redfee']=intval($totalmoney*$rate)/100;
	$return['bonus_wait']=$return['member']['bonus']?intval($redfee*$return['member']['bonus'])/100:0;
	$return['bonustotal']=pdo_getsum('jy_globonus_billp',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid']),'realmoney');
	$return['bonusok']=pdo_getsum('jy_globonus_billp',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid'],'status'=>1),'realmoney');
	$return['bonuslock']=price($return['bonustotal']-$return['bonusok']);
	if($this->setting['zhouqi']!=2){
		$firstday =  strtotime(date("Y-m-01", time()));
	}else{
		$day=date("d", time());
		$firstday = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		$firstday =strtotime($firstday);
	}
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$firstday);
	$list=pdo_getall('jy_weishop_order',$params,array('id','price'));
	$allredfee=0;
	foreach($list as $item){
		$bonusmoney=$this->model('model')->get_redfee($item);
		$allredfee +=$bonusmoney;
	}
	$return['allredfee']=$allredfee;
	if($_SESSION['onemerchid']){
		$return['merch']=pdo_get('jy_merch_user',array('id'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	}
	set_html('title',$return['set']['tcenter']?:'股东中心');
	exi($return);
}
if($_RQ['op']=='list'){
	if($this->setting['zhouqi']!=2){
		$firstday =  strtotime(date("Y-m-01", time()));
	}else{
		$day=date("d", time());
		$firstday = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		$firstday =strtotime($firstday);
	}
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$firstday);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['uid']);
			$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
			$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
			$item['total']=$goods['total'];
			$item['title']=$goodsinfo['title'];
			$item['image']=$goodsinfo['image'];
			$item['unit']=$goodsinfo['unit'];
			if(!$item['merchid'] && $item['storeid']){
				$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid']),'title');
			}
			if($item['merchid']){
				$item['storetitle']='商户';
			}
			$item['storetitle']=$item['storetitle']?:'平台自营';
			$item['bonusmoney']=$this->model('model')->get_redfee($item);
			$item['statustitle']=$this->billp_status[$item['status']];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}